package core;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Shape;
/*
 * Defines a shape, either a rectangle or an oval, and it's position, color and size.
 */
public class PShape{
	private Color bc;
	private double x;
	private double y;
	private double height;
	private double width;
	private String sh;
	
	/**
	 * Draws this shape onto the given Graphics.
	 * @param g The Graphics object to draw the shape on.
	 */
	public void draw(Graphics g){
		g.setColor(bc);
		if (sh.equals("FOVAL")) g.fillOval((int)x, (int)y, (int)width, (int)height);	
		else if (sh.equals("FRECT")) g.fillRect((int)x, (int)y, (int)width, (int)height);	
		else if (sh.equals("RECT")) g.drawRect((int)x, (int)y, (int)width, (int)height);
		else if (sh.equals("OVAL")) g.drawOval((int)x, (int)y, (int)width, (int)height);
		else if (sh.equals("LINE")) g.drawLine((int)x, (int)y, (int)(x+width), (int)(y+height));
	}
	/**
	 * Checks if a point is contained within this shape
	 * @param ix The X-value to check
	 * @param iy The Y-value to check
	 * @return Returns true if the x and y value are within the shape
	 */
	public boolean contains(int ix, int iy){
		if (ix >= x && ix<= x+width && iy >=y && iy<=y+height) return true;
		else return false;
	}
	/**
	 * Constructor takes all the properties of a PShape.
	 * @param c The Color of the shape
	 * @param xi The x-axis position of the shape
	 * @param yi The y-axis position of the shape
	 * @param h The height of the shape
	 * @param w The width of the shape
	 * @param s A string representing the type of shape to be used
	 */
	public PShape(Color c, int xi, int yi, int w, int h, String s){
		sh=s.toUpperCase();
		bc=c;
		x=xi;
		y=yi;
		height=h;
		width=w;
	}
	/**
	 * Constructor takes the description of a PShape from a single String, as presented in toString().
	 * @param s The String describing the PShape
	 */
	public PShape (String s) {
		String[] t = s.split(",");
		sh=t[5];
		bc=Color.decode(t[0]);
		x=Double.parseDouble(t[1]);
		y=Double.parseDouble(t[2]);
		height=Double.parseDouble(t[3]);
		width=Double.parseDouble(t[4]);
	}
	/**
	 * Returns a string describing all attributes of this PShape.
	 */
	public String toString(){
		return String.valueOf(bc.getRGB()) + "," + x + "," + y + "," + height + "," + width + "," + sh;
	}

	public void setX(double nx){	
		x = nx;
	}
	
	public void setY(double ny){
		y = ny;
	}
	
	public void setHeight(double nh){
		if (nh>1) height = nh;
		else height=1;
	}
		
	public void setWidth(double nw){
		if (nw>1) width = nw;
		else width=1;
	}
	
	public double getX(){	
		return x;
	}
	
	public double getY(){	
		return y;
	}
	
	public double getWidth(){	
		return width;
	}
	
	public double getHeight(){	
		return height;
	}
	
	public String getShape(){
		return sh;
	}
	
}
